package br.unesp.demac.bcc.soo.sistemaclarus.view;

import br.unesp.demac.bcc.soo.sistemaclarus.bean.Localizacao;
import br.unesp.demac.bcc.soo.sistemaclarus.bean.Obra;
import br.unesp.demac.bcc.soo.sistemaclarus.dao.LocalizacaoDAOImp;
import br.unesp.demac.bcc.soo.sistemaclarus.dao.ObraDAOImp;
import br.unesp.demac.bcc.soo.sistemaclarus.util.FrameUtil;
import java.sql.SQLException;
import java.util.List;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author Evaldo
 */
public class JFrameAlteracaoObra extends javax.swing.JFrame {

    /** Creates new form JFrameAlteracaoObra */
    public JFrameAlteracaoObra() {
        initComponents();
        
        popularTabela();
        
        FrameUtil.centralizar(this);
    }
    
    private void popularTabela(){
        JComboBox cbxLoc = new JComboBox();
        List<Obra> lstObras = null;
        
        try{
            ObraDAOImp obDao = new ObraDAOImp();
            lstObras = obDao.buscarObras();
            
            LocalizacaoDAOImp locDao = new LocalizacaoDAOImp();    
            List<Localizacao> lstLocalizacao = locDao.buscaTodasLocalizacoes();            
            for (int i = 0; i < lstLocalizacao.size(); i++){
                cbxLoc.addItem(lstLocalizacao.get(i));
                jComboBox1.addItem(lstLocalizacao.get(i));
            }
            
            DefaultTableModel model = (DefaultTableModel)tblObras.getModel();
            cbxLoc.setSelectedItem(lstObras.get(lstObras.size() - 1).getLocalizacao());
            jComboBox1.setSelectedItem(lstObras.get(lstObras.size() - 1).getLocalizacao());
            
            for (Obra obr : lstObras){                
                model.addRow(new String[] { obr.getNomeObra(), String.valueOf(obr.getAnoPublicacao()) });
            }
            
            
            TableColumn columnLoc = tblObras.getColumnModel().getColumn(2);
            columnLoc.setCellEditor(new DefaultCellEditor(cbxLoc));
        }
        catch(SQLException ex){}      
       
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblObras = new javax.swing.JTable();
        btnSalvar = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        tblObras.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Obra", "Ano de Publicação", "Localização"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblObras);

        btnSalvar.setText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 481, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 430, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 347, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addComponent(btnSalvar))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        DefaultTableModel model = (DefaultTableModel)tblObras.getModel();
        
    }//GEN-LAST:event_btnSalvarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new JFrameAlteracaoObra().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSalvar;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblObras;
    // End of variables declaration//GEN-END:variables
}
